package com.school.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.school.exception.UserNotFoundException;
import com.school.model.Pics;
import com.school.model.Sign;
import com.school.model.User;
import com.school.service.impl.PicsServiceImpl;
import com.school.service.impl.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Component
public class FileUtil {
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private PicsServiceImpl picsService;

    @Value("${file.path}")
    private String filePath;
    @Value("${file.certifications.tmp}")
    private String fileCertificationsTmp;

    public String downloadCertification(HttpServletResponse response, Sign sign) throws UserNotFoundException {
        List<Pics> userLogos = picsService.querySelective(null, sign.getSignUserId(), FileEnum.LOGO.value());
        if (userLogos.size() == 0) {
            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "还未上传学校logo！");
        }
        List<Pics> userSignatures = picsService.querySelective(null, sign.getSignUserId(), FileEnum.SIGNATURE.value());
        if (userSignatures.size() == 0) {
            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "还未上传学校校长签章！");
        }
        List<Pics> useredLogos = picsService.querySelective(null, sign.getSignedUserId(), FileEnum.LOGO.value());
        if (useredLogos.size() == 0) {
            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "目标学校还未上传学校logo！");
        }
        List<Pics> useredSignatures = picsService.querySelective(null, sign.getSignedUserId(), FileEnum.SIGNATURE.value());
        if (useredSignatures.size() == 0) {
            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "目标学校还未上传学校校长签章！");
        }
//        List<Pics> witnesses = picsService.querySelective(null, null, FileEnum.WITNESS_LOGO.value());
//        if (witnesses.size() == 0) {
//            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "见证人的logo不存在！");
//        }
        User user = userService.findById(sign.getSignUserId());
        User usered = userService.findById(sign.getSignedUserId());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        String country1 = user.getCountry();
        String logo1 = filePath + userLogos.get(0).getLocation();
        String signature1 = filePath + userSignatures.get(0).getLocation();
        String school1 = user.getSchoolName();
        String job1 = user.getProfession();
        String time1 = dateTimeFormatter.format(LocalDateTime.now());
//        String name1 = user.getContact();

        String country2 = usered.getCountry();
        String logo2 = filePath + useredLogos.get(0).getLocation();
        String signature2 = filePath + useredSignatures.get(0).getLocation();
        String school2 = usered.getSchoolName();
        String job2 = usered.getProfession();
        String time2 = dateTimeFormatter.format(LocalDateTime.now());
//        String name2 = usered.getContact();

//        String witness = filePath + witnesses.get(witnesses.size() - 1).getLocation();
//        String witness = filePath + witnessFileName;

//        File file = new File(witness);
//        if (!file.exists()) {
//            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "见证者的logo未上传，且需命名为${file.name.witness}！");
//        }
        Map<String, Object> datas = fillData(
                country1, logo1, school1, job1, time1, signature1,
                country2, logo2, school2, job2, time2, signature2,
                null
        );
        try {
            render(datas, response);
        } catch (IOException | NullPointerException e) {
            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), e.getMessage() + "生成签约证书失败！");
        }
        return ResponseUtil.build(HttpStatus.OK.value(), "生成签约证书成功！");
    }

    private void render(Map<String, Object> datas, HttpServletResponse response) throws IOException, NullPointerException {
//        List<Pics> templates = picsService.querySelective(null, null, FileEnum.TEMPLATE.value(), null, null);

//        if (templates.size() == 0) {
//            throw new NullPointerException("管理员尚未上传签约证书模板！");
//        }
//        Pics theTemplate = templates.get(0);
        String templateLocation = filePath + "tmp.docx";
        XWPFTemplate template = XWPFTemplate.compile(new File(templateLocation))
                .render(datas);

        String school1 = (String) datas.get("school1");
        String school2 = (String) datas.get("school2");
        String name = "签约证书(" + school1 + "-" + school2 + ")";
        response.setHeader("Content-disposition", "attachment;filename=" + new String(name.getBytes(StandardCharsets.UTF_8), "ISO8859-1") + ".docx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("UTF-8");
        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        template.write(outputStream);
        template.close();
        outputStream.close();
    }

    private Map<String, Object> fillData(String country1, String logo1, String school1, String job1, String time1, String signature1,
                                         String country2, String logo2, String school2, String job2, String time2, String signature2,
                                         String logo3) {
        Map<String, Object> datas = new HashMap<String, Object>() {
            {
                put("country1", country1);
                put("country2", country2);
                put("school1", school1);
                put("school2", school2);
                put("job1", job1);
                put("job2", job2);

                String formatLogo1 = logo1.substring(logo1.lastIndexOf("."));
                String formatLogo2 = logo2.substring(logo2.lastIndexOf("."));

                put("logo1", new PictureRenderData(90, 90, formatLogo1, BytePictureUtils.getLocalBufferedImage(new File(logo1))));
                put("logo2", new PictureRenderData(90, 90, formatLogo2, BytePictureUtils.getLocalBufferedImage(new File(logo2))));

                //最顶部公用的图片
                String formatSignature1 = logo1.substring(logo1.lastIndexOf("."));
                String formatSignature2 = logo2.substring(logo2.lastIndexOf("."));
                put("name1", new PictureRenderData(120, 50, formatSignature1, BytePictureUtils.getLocalBufferedImage(new File(signature1))));
                put("name2", new PictureRenderData(120, 50, formatSignature2, BytePictureUtils.getLocalBufferedImage(new File(signature2))));

//                put("logo3", "http://124.156.153.105/files/AUPFLOGO_1.jpg");
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(date);
                put("date1", time);
                put("date2", time);
                put("logo3", new PictureRenderData(90, 90, ".jpg", BytePictureUtils.getLocalBufferedImage(new File(filePath + "AUPFLOGO_1.jpg"))));
                put("logo4", new PictureRenderData(520, 90, ".jpg", BytePictureUtils.getLocalBufferedImage(new File(filePath + "AUPFLOGO_2.jpg"))));
//
//                put("name1", name1);
//                put("name2", name2);
            }
        };
        return datas;
    }


    //此方法其实和上面那个作用差不多，但由于若抽取处相同的方法，会使得不知道如何处理上述的弱智异常判断，不信自己试试，我也了解得到了，要多抛异常，少自己判断，学到了！！！
    public void downloadCertification(Sign sign) throws IllegalArgumentException, UserNotFoundException, IOException {
        List<Pics> userLogos = picsService.querySelective(null, sign.getSignUserId(), FileEnum.LOGO.value());
        if (userLogos.size() == 0) {
            throw new IllegalArgumentException("主动签约方未上传学校logo");
        }
        List<Pics> userSignatures = picsService.querySelective(null, sign.getSignUserId(), FileEnum.SIGNATURE.value());
        if (userSignatures.size() == 0) {
            throw new IllegalArgumentException("还未上传学校校长签章");
        }
        List<Pics> useredLogos = picsService.querySelective(null, sign.getSignedUserId(), FileEnum.LOGO.value());
        if (useredLogos.size() == 0) {
            throw new IllegalArgumentException("目标学校还未上传学校logo");
        }
        List<Pics> useredSignatures = picsService.querySelective(null, sign.getSignedUserId(), FileEnum.SIGNATURE.value());
        if (useredSignatures.size() == 0) {
            throw new IllegalArgumentException("目标学校还未上传学校校长签章");
        }
        User user = userService.findById(sign.getSignUserId());
        User usered = userService.findById(sign.getSignedUserId());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        String country1 = user.getCountry();
        String logo1 = filePath + userLogos.get(0).getLocation();
        String signature1 = filePath + userSignatures.get(0).getLocation();
        String school1 = user.getSchoolName();
        String job1 = user.getProfession();
        String time1 = dateTimeFormatter.format(LocalDateTime.now());
//        String name1 = user.getContact();

        String country2 = usered.getCountry();
        String logo2 = filePath + useredLogos.get(0).getLocation();
        String signature2 = filePath + useredSignatures.get(0).getLocation();
        String school2 = usered.getSchoolName();
        String job2 = usered.getProfession();
        String time2 = dateTimeFormatter.format(LocalDateTime.now());
//        String name2 = usered.getContact();

//        String witness = filePath + witnesses.get(witnesses.size() - 1).getLocation();
//        String witness = filePath + witnessFileName;

//        File file = new File(witness);
//        if (!file.exists()) {
//            return ResponseUtil.build(HttpStatus.BAD_REQUEST.value(), "见证者的logo未上传，且需命名为${file.name.witness}！");
//        }
        Map<String, Object> datas = fillData(
                country1, logo1, school1, job1, time1, signature1,
                country2, logo2, school2, job2, time2, signature2,
                null
        );

        String templateLocation = filePath + "tmp.docx";
        XWPFTemplate template = XWPFTemplate.compile(new File(templateLocation))
                .render(datas);

        String name = "签约证书(" + school1 + "-" + school2 + ")";
        //写到本地中
        String directory = filePath + fileCertificationsTmp;
        File file = new File(directory);
        if (!file.exists()) {
            file.mkdir();
        }
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(filePath + fileCertificationsTmp + name + ".docx")));
        template.write(outputStream);
//        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//        template.write(outputStream);
        template.close();
        outputStream.close();
    }

    public static String folderToZip(String src, String fileName, String target) throws IOException {
        File srcFile = new File(src);
        if (!srcFile.exists()) {
            logger.error(src + "不存在！");
            throw new FileNotFoundException(src + "->文件不存在！");
        }
        File[] files = srcFile.listFiles();
        assert files != null;
        if (files.length == 0) {
            logger.error(src + "文件夹中内容为空！");
            throw new FileNotFoundException(src + "->文件夹内容为空");
        }
        File zipFile = new File(target + fileName + ".zip");
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)))) {
            compress(zipOutputStream, srcFile, srcFile.getName());
        }
        return zipFile.getAbsolutePath();
    }

    private static void compress(ZipOutputStream zipOutputStream, File srcFile, String base) throws IOException {
        if (srcFile.isDirectory()) {
            File[] files = srcFile.listFiles();
            if (files.length == 0) {
                zipOutputStream.putNextEntry(new ZipEntry(base));
            } else {
                for (File file : files) {
                    compress(zipOutputStream, file, base + "/" + file.getName());
                }
            }
        } else {
            zipOutputStream.putNextEntry(new ZipEntry(base));
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
            byte[] bytes = new byte[1024 * 2];
            while (bufferedInputStream.read(bytes, 0, bytes.length) != -1) {
                zipOutputStream.write(bytes);
            }
            bufferedInputStream.close();
        }
    }

    public void downloadCertification2(HttpServletResponse response, Sign sign) throws IOException, DocumentException {
        User u1 = userService.queryById(sign.getSignUserId(), User.Column.id, User.Column.schoolName);
        User u2 = userService.queryById(sign.getSignedUserId(), User.Column.id, User.Column.schoolName);
        List<Pics> u1Logos = picsService.querySelective(u1.getId(), FileEnum.LOGO.value());
        if (u1Logos.size() == 0) {
            throw new NullPointerException("logo不存在！");
        }
        List<Pics> u2Logos = picsService.querySelective(u2.getId(), FileEnum.LOGO.value());
        if (u2Logos.size() == 0) {
            throw new NullPointerException("logo不存在！");
        }
        String mouPdf = filePath + "mou.pdf";
        String logo1 = filePath + u1Logos.get(0).getLocation();
        String logo2 = filePath + u2Logos.get(0).getLocation();
        String schoolName1 = u1.getSchoolName();
        String schoolName2 = u2.getSchoolName();
//        String targetPath = filePath + fileCertificationsTmp + schoolName1 + "-" + schoolName2 + ".pdf";
//        DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(targetPath)));
        String name = "签约证书(" + schoolName1 + "-" + schoolName2 + ")" + ".pdf";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "filename=" + new String(name.getBytes(), "iso8859-1"));
        try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(response.getOutputStream()));) {
            PdfStamper pdfStamper = new PdfStamper(new PdfReader(mouPdf), dataOutputStream);
            PdfContentByte pdfContentByte = pdfStamper.getOverContent(1);
            insertImages(logo1, logo2, pdfContentByte);
            insertText(schoolName1, schoolName2, pdfContentByte);
            pdfStamper.close();
        }
    }

    public void downloadCertification3(Sign sign, HttpServletResponse response) throws IOException, DocumentException {
        User u1 = userService.queryById(sign.getSignUserId(), User.Column.id, User.Column.schoolName);
        User u2 = userService.queryById(sign.getSignedUserId(), User.Column.id, User.Column.schoolName);
        List<Pics> u1Logos = picsService.querySelective(u1.getId(), FileEnum.LOGO.value());
        Assert.notEmpty(u1Logos, "用户未上传logo");
        List<Pics> u2Logos = picsService.querySelective(u2.getId(), FileEnum.LOGO.value());
        Assert.notEmpty(u2Logos, "用户未上传logo");
        String mouPdf = filePath + "mou.pdf";
        String schoolName1 = u1.getSchoolName();
        String schoolName2 = u2.getSchoolName();
        String name = "签约证书(" + u1.getId() + "-" + u2.getId() + ")" + ".pdf ";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "filename=" + new String(name.getBytes(), "iso8859-1"));
        try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(response.getOutputStream()));) {
            PdfStamper pdfStamper = new PdfStamper(new PdfReader(mouPdf), dataOutputStream);
            PdfContentByte pdfContentByte = pdfStamper.getOverContent(1);
            insertCustomImages(u1Logos.get(0), u2Logos.get(0), pdfContentByte);
//            insertCustomFakeImage();
            insertText(schoolName1, schoolName2, pdfContentByte);
            pdfStamper.close();
        }
    }

    /**
     * 此方法同下面的下载方法，但我不想重构了，因此放着
     *
     * @param logo1          //     * @param logo1X
     *                       //     * @param logo1Y
     *                       //     * @param logo1Width
     *                       //     * @param logo1Height
     * @param logo2
     * @param pdfContentByte
     * @throws IOException
     * @throws DocumentException
     */
    private void insertCustomImages(Pics logo1, Pics logo2, PdfContentByte pdfContentByte) throws IOException, DocumentException {
        int widthOfPdf = 720;
        int absoluteY1 = 205;
        int absoluteY2 = 205;
//        content.beginText();
        Image instanceLogo1 = null;
        Image instanceLogo2 = null;
        TrueAttributeOfImage trueAttributeOfLogo1 = new TrueAttributeOfImage();
        TrueAttributeOfImage trueAttributeOfLogo2 = new TrueAttributeOfImage();
        if (logo1.getTransparency().equals(Status.TRANSPARENCY)) {
            byte[] bytesLogo1 = transferAlpha(filePath + logo1.getLocation(), trueAttributeOfLogo1);
            instanceLogo1 = Image.getInstance(bytesLogo1);
        } else {
            instanceLogo1 = Image.getInstance(filePath + logo1.getLocation());
        }
        if (logo2.getTransparency().equals(Status.TRANSPARENCY)) {
            byte[] bytesLogo2 = transferAlpha(filePath + logo2.getLocation(), trueAttributeOfLogo2);
            instanceLogo2 = Image.getInstance(bytesLogo2);
        } else {
            instanceLogo2 = Image.getInstance(filePath + logo2.getLocation());
        }

        instanceLogo1.setScaleToFitHeight(true);
        instanceLogo1.setCompressionLevel(9);
//        imageLogo1.setScaleToFitLineWhenOverflow(true);

        instanceLogo2.setCompressionLevel(9);
//        imageLogo2.setScaleToFitLineWhenOverflow(true);
//        int realHeightOfLogo1 = trueAttributeOfLogo1.getHeight() == null ? (int) instanceLogo1.getHeight() : trueAttributeOfLogo1.getHeight();
//        int realWidthOfLogo1 = trueAttributeOfLogo1.getWidth() == null ? (int) instanceLogo1.getWidth() : trueAttributeOfLogo1.getWidth();
//        int realHeightOfLogo2 = trueAttributeOfLogo2.getHeight() == null ? (int) instanceLogo2.getHeight() : trueAttributeOfLogo2.getHeight();
//        int realWidthOfLogo2 = trueAttributeOfLogo2.getWidth() == null ? (int) instanceLogo2.getWidth() : trueAttributeOfLogo2.getWidth();

        //若用户未设置logo的宽和高，默认高位140
//        int heightOfLogo1 = logo1.getHeight() == 0 ? 140 : logo1.getHeight();
        float heightOfLogo1 = instanceLogo1.getScaledHeight() > 180 ? 140 : instanceLogo1.getScaledHeight();
//        int widthOfLogo1 = logo1.getWidth() == 0 ? heightOfLogo1 * realWidthOfLogo1 / realHeightOfLogo1 : logo1.getWidth();
        float widthOfLogo1 = instanceLogo1.getScaledWidth() > 245 ? (heightOfLogo1 * instanceLogo1.getScaledWidth() / instanceLogo1.getScaledHeight()) : instanceLogo1.getScaledWidth();
        instanceLogo2.setScaleToFitHeight(true);
        //以下判断用于解决倘若在原始高度140px情况下超过了pdf的长度的一半就调整
        int bonusLogo1Left = 90;
//        if (widthOfLogo1 >= (widthOfPdf >> 1) - 30) {
//            bonusLogo1Left = (int) (bonusLogo1Left - 30);
//            heightOfLogo1 = heightOfLogo1 >> 1;
//            widthOfLogo1 = heightOfLogo1 * realWidthOfLogo1 / realHeightOfLogo1;
//            absoluteY1 = (int) (absoluteY1 * 1.15);
//        }
        instanceLogo1.scaleToFit(widthOfLogo1, heightOfLogo1);
//        instanceLogo1.setAbsolutePosition(logo1.getOffsetX() + bonusLogo1Left + (Math.max((245 - widthOfLogo1), 20) >> 1), logo1.getOffsetY() + absoluteY1);
        instanceLogo1.setAbsolutePosition(bonusLogo1Left + ((245 - widthOfLogo1) / 2), absoluteY1);
//        int heightOfLogo2 = logo2.getHeight() == 0 ? 140 : logo2.getHeight();
        float heightOfLogo2 = instanceLogo2.getScaledHeight() > 180 ? 140 : instanceLogo2.getScaledHeight();
//        float widthOfLogo2 = instanceLogo2.getScaledWidth();
        float widthOfLogo2 = instanceLogo2.getScaledWidth() > 245 ? (heightOfLogo2 * instanceLogo2.getScaledWidth() / instanceLogo2.getScaledHeight()) : instanceLogo2.getScaledWidth();
        int bonusLogo2Left = 360 + 25;
//        if (widthOfLogo2 >= ((widthOfPdf >> 1) - 30)) {
//            bonusLogo2Left = (int) (bonusLogo2Left - 20);
//            heightOfLogo2 = heightOfLogo2 >> 1;
//            widthOfLogo2 = heightOfLogo2 * realWidthOfLogo2 / realHeightOfLogo2;
//            absoluteY2 = (int) (absoluteY2 * 1.15);
//        }
        instanceLogo2.scaleToFit(widthOfLogo2, heightOfLogo2);
        instanceLogo2.setAbsolutePosition(bonusLogo2Left + (245 - widthOfLogo2) / 2, absoluteY2);
        //此时得到了适配pdf模板的图片的长和宽,需要计算其x值
        //第一种情况，长不会超过规定的区域 (360+360)*420,已解决，现在解决第二种情况，就是有些图片的长度明显比高度高
        //默认有90px的偏差，40px为中间那个图标的偏差
//        System.out.println("第一张logo的x坐标" + (90 + (Math.max((245 - widthOfLogo1), 20) >> 1)));
//        instanceLogo2.setAbsolutePosition(logo2.getOffsetX() + bonusLogo2Left + (Math.max((245 - widthOfLogo2), 20) / 2), logo2.getOffsetY() + absoluteY2);
        pdfContentByte.addImage(instanceLogo1);
        pdfContentByte.addImage(instanceLogo2);
    }

    public void downloadCertificationFake(Integer userId, Integer offsetX, Integer offsetY, Integer width, Integer height, HttpServletResponse response) throws IOException, DocumentException {
        List<Pics> logos = picsService.querySelective(userId, FileEnum.LOGO.value());
        Assert.notEmpty(logos, "当前用户未上传logo");
        Pics logo = logos.get(0);
        String mouPdf = filePath + "mou-fake.pdf";
//        String logo1 = filePath + u1Logos.get(0).getLocation();
//        String logo2 = filePath + u2Logos.get(0).getLocation();
//        String targetPath = filePath + fileCertificationsTmp + schoolName1 + "-" + schoolName2 + ".pdf";
//        DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(targetPath)));
        String name = "mou-fake" + ".pdf";
//        String name = "mou" + ".pdf";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "filename=" + new String(name.getBytes(), "iso8859-1"));
        try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(response.getOutputStream()));) {
            PdfStamper pdfStamper = new PdfStamper(new PdfReader(mouPdf), dataOutputStream);
            PdfContentByte pdfContentByte = pdfStamper.getOverContent(1);
            insertFakeImage(logo, offsetX, offsetY, width, height, pdfContentByte);
            pdfStamper.close();
        }
    }

    private void insertFakeImage(Pics logo, Integer offsetX, Integer offsetY, Integer width, Integer height, PdfContentByte pdfContentByte) throws IOException, DocumentException {
        TrueAttributeOfImage trueAttributeOfLogo1 = new TrueAttributeOfImage();
//        TrueAttributeOfImage trueAttributeOfLogo2 = new TrueAttributeOfImage();
        byte[] bytesLogo = transferAlpha(filePath + logo.getLocation(), trueAttributeOfLogo1);
//        byte[] bytesLogo2 = transferAlpha(logo2, trueAttributeOfLogo2);
        Image instance = Image.getInstance(bytesLogo);
        instance.setScaleToFitHeight(true);
//        instance.scaleToFit(width == 0 ? trueAttributeOfLogo1.getScaledWidth() : width, height == 0 ? trueAttributeOfLogo1.getScaledHeight() > 180 ? 140 : trueAttributeOfLogo1.getScaledHeight() : height);
        float finalHeight = instance.getScaledHeight() > 180 ? 140 : instance.getHeight();
        float finalWidth = instance.getScaledWidth() > 245 ? (finalHeight * instance.getScaledWidth() / instance.getScaledHeight()) : instance.getScaledWidth();
//        float finalWidth = instance.getWidth() > 245 ? 140 : instance.getWidth();
        instance.scaleToFit(finalWidth, finalHeight);
//        bonusLogo2Left + (245 - widthOfLogo2) / 2
        instance.setAbsolutePosition(90 + offsetX + ((245 - finalWidth) / 2), 225 - 20 + offsetY);
        pdfContentByte.addImage(instance);
    }

    static class TrueAttributeOfImage {
        private Integer height;
        private Integer width;

        public Integer getHeight() {
            return height;
        }

        public void setHeight(Integer height) {
            this.height = height;
        }

        public Integer getWidth() {
            return width;
        }

        public void setWidth(Integer width) {
            this.width = width;
        }
    }

    private static void insertImages(String logo1, String logo2, PdfContentByte pdfContentByte) throws IOException, DocumentException {
        int widthOfPdf = 720;
        int absoluteY1 = 225;
        int absoluteY2 = 225;
//        content.beginText();
        TrueAttributeOfImage trueAttributeOfLogo1 = new TrueAttributeOfImage();
        TrueAttributeOfImage trueAttributeOfLogo2 = new TrueAttributeOfImage();
        byte[] bytesLogo1 = transferAlpha(logo1, trueAttributeOfLogo1);
        byte[] bytesLogo2 = transferAlpha(logo2, trueAttributeOfLogo2);
        //分别得到了两张图片的真实宽和高
        Image imageLogo1 = Image.getInstance(bytesLogo1);
        Image imageLogo2 = Image.getInstance(bytesLogo2);
        imageLogo1.setScaleToFitHeight(true);
//        imageLogo1.setScaleToFitLineWhenOverflow(true);
        imageLogo1.setCompressionLevel(9);
        imageLogo2.setCompressionLevel(9);
        imageLogo2.setScaleToFitHeight(true);
//        imageLogo2.setScaleToFitLineWhenOverflow(true);
        int realHeightOfLogo1 = trueAttributeOfLogo1.getHeight();
        int realWidthOfLogo1 = trueAttributeOfLogo1.getWidth();
        int realHeightOfLogo2 = trueAttributeOfLogo2.getHeight();
        int realWidthOfLogo2 = trueAttributeOfLogo2.getWidth();

        int heightOfLogo1 = 140;
        heightOfLogo1 = (int) imageLogo1.getHeight();
        int widthOfLogo1 = heightOfLogo1 * realWidthOfLogo1 / realHeightOfLogo1;
        //以下判断用于解决倘若在原始高度140px情况下超过了pdf的长度的一半就调整
        int bonusLogo1Left = 90;
        if (widthOfLogo1 >= (widthOfPdf >> 1) - 30) {
            bonusLogo1Left = (int) (bonusLogo1Left - 30);
            heightOfLogo1 = heightOfLogo1 >> 1;
            widthOfLogo1 = heightOfLogo1 * realWidthOfLogo1 / realHeightOfLogo1;
            absoluteY1 = (int) (absoluteY1 * 1.15);
        }
        imageLogo1.scaleToFit(widthOfLogo1, heightOfLogo1);

        int heightOfLogo2 = 140;
        heightOfLogo2 = (int) imageLogo2.getHeight();
        int widthOfLogo2 = heightOfLogo2 * realWidthOfLogo2 / realHeightOfLogo2;
        int bonusLogo2Left = 385;
        if (widthOfLogo2 >= ((widthOfPdf >> 1) - 30)) {
            bonusLogo2Left = (int) (bonusLogo2Left - 20);
            heightOfLogo2 = heightOfLogo2 >> 1;
            widthOfLogo2 = heightOfLogo2 * realWidthOfLogo2 / realHeightOfLogo2;
            absoluteY2 = (int) (absoluteY2 * 1.15);
        }
        imageLogo2.scaleToFit(widthOfLogo2, heightOfLogo2);
        //此时得到了适配pdf模板的图片的长和宽,需要计算其x值
        //第一种情况，长不会超过规定的区域 (360+360)*420,已解决，现在解决第二种情况，就是有些图片的长度明显比高度高
        //默认有90px的偏差，40px为中间那个图标的偏差
        imageLogo1.setAbsolutePosition(bonusLogo1Left + (Math.max((245 - widthOfLogo1), 20) >> 1), absoluteY1);
//        System.out.println("第一张logo的x坐标" + (90 + (Math.max((245 - widthOfLogo1), 20) >> 1)));

        imageLogo2.setAbsolutePosition(bonusLogo2Left + (Math.max((245 - widthOfLogo2), 20) >> 1), absoluteY2);
//        System.out.println("第二张logo的x坐标:" + (385 + (Math.max((245 - widthOfLogo2), 20) >> 1)));

        pdfContentByte.addImage(imageLogo1);
        pdfContentByte.addImage(imageLogo2);
    }

    private static void insertText(String schoolName1, String schoolName2, PdfContentByte pdfContentByte) throws IOException, DocumentException {
        BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        pdfContentByte.setColorFill(new BaseColor(9,80,158));
        pdfContentByte.beginText();
        pdfContentByte.setFontAndSize(font, 24);
        int logo1X = 0;
        float w1 = pdfContentByte.getEffectiveStringWidth(schoolName1, false);
        int widthOfPdf = 721;
        logo1X = (int) ((widthOfPdf - w1) / 2);
        pdfContentByte.setTextMatrix(logo1X, 144);
        pdfContentByte.showText(schoolName1);
        float w2 = pdfContentByte.getEffectiveStringWidth(schoolName2, false);
        pdfContentByte.setFontAndSize(font, 24);
        int logo2X = (int) ((widthOfPdf - (w2)) / 2);
        pdfContentByte.setTextMatrix(logo2X, 96);
        pdfContentByte.showText(schoolName2);
        pdfContentByte.endText();
    }

    private static byte[] transferAlpha(String imagePath, TrueAttributeOfImage attributeOfImage) throws IOException {
//        String format = imagePath.substring(imagePath.lastIndexOf(".") + 1);
        BufferedImage b = ImageIO.read(new FileInputStream(imagePath));
        ImageIcon imageIcon = new ImageIcon(b);
        BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
        graphics2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
        int alpha = 0;
        int x1 = bufferedImage.getWidth(), y1 = bufferedImage.getHeight(), x2 = 0, y2 = 0;
        for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
            for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int R = (rgb & 0xff0000) >> 16;
                int G = (rgb & 0xff00) >> 8;
                int B = (rgb & 0xff);
                if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
                    rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                } else {
                    x1 = Math.min(x, x1);
                    x2 = Math.max(x2, x);
                    y1 = Math.min(y1, y);
                    y2 = Math.max(y2, y);
                }
                bufferedImage.setRGB(x, y, rgb);
            }
        }
//        System.out.println("height:" + bufferedImage.getHeight());
//        System.out.println("y2-y1:" + (y2 - y1));
//        System.out.println("width:" + bufferedImage.getWidth());
//        System.out.println("x2-x1:" + (x2 - x1));
        attributeOfImage.setHeight(y2 - y1);
        attributeOfImage.setWidth(x2 - x1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        graphics2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
//        return bufferedImage;
//        ImageIO.write(bufferedImage, "png", new File("C:\\Users\\Administrator\\Desktop\\" + System.currentTimeMillis() + "." + format));
        boolean png = ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();

    }
}
